//Write by oshogbo//vx
//23.07.2011
//http://code.google.com/p/streamobs

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class SettingsWindow extends JFrame implements Serializable 
{
  private static final long serialVersionUID = 1L;

  private static final int sizeInRow = 8;
  private static final int textFiledWidth = 15;
  private static final String fileSettingsName = "stream.set";

  private JTextField nameOfStream[] = new JTextField[sizeInRow];
  private JTextField nameOfChannel[] = new JTextField[sizeInRow];
  private ServerType serverType[] = new ServerType[sizeInRow];
  private TimeSetting timeSetting = new TimeSetting();
  transient private ArrayList<Channels> channelList = new ArrayList<Channels>();

  public SettingsWindow()
  {
    super("Setting Window");
    setSize(new Dimension(520, 330));
    //setUndecorated(true);
    //getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); 
    setResizable( false );    
    setIconImage(new ImageIcon("gfx/SCicosmall.jpg").getImage());

    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);  
    JPanel top = new JPanel();

    top.setLayout(new GridLayout(0, 3));
    setLayout(new FlowLayout());

    top.add(new JLabel("Name to show"));
    top.add(new JLabel("Name of channel"));
    top.add(new JLabel("Server"));

    for(int i = 0; i < sizeInRow; i ++)
    {
      nameOfStream[i] = new JTextField(textFiledWidth);
      top.add(nameOfStream[i]);

      nameOfChannel[i] = new JTextField(textFiledWidth);
      top.add(nameOfChannel[i]);

      serverType[i] = new ServerType();
      top.add(serverType[i]); 
    }
   
    top.add( new JLabel("Refresh time: ") ); 
    top.add(timeSetting);

    this.getContentPane().add(top);

    JButton newButton;

    newButton = new JButton("Save");
    newButton.addActionListener( new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
	SettingsWindow.this.createChanelsList();
	SettingsWindow.this.saveSettings();
	SettingsWindow.this.setVisible(false);
      }
    });

    this.getContentPane().add(newButton);
    
    newButton = new JButton("Cancel");
    newButton.addActionListener( new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
	SettingsWindow.this.setVisible(false);
      }
    });
    this.getContentPane().add(newButton);

  }

  public void setVisible(boolean b)
  {
    if(b == true && !isVisible())
      loadSettings();

    super.setVisible(b);
  }

  public void loadSettings()
  {
    File pfile = new File(fileSettingsName);

    if(pfile.exists())
    {
      try
      {
	FileInputStream fis = new FileInputStream(pfile);	
	ObjectInputStream ois = new ObjectInputStream(fis);
	change( (SettingsWindow)ois.readObject() );
      }catch( IOException e ) 
      {
	e.printStackTrace();
      }catch( ClassNotFoundException e)
      {
	e.printStackTrace();
      }
    }
  }

  private void change(SettingsWindow sw)
  {
    for(int i = 0; i < sizeInRow; i ++)
    {
      nameOfStream[i].setText( sw.nameOfStream[i].getText() );
      nameOfChannel[i].setText( sw.nameOfChannel[i].getText() );
      serverType[i].setSelectedItem( sw.serverType[i].getNumberOfSelectedItem() );
    }

    timeSetting.setSelectedItem( sw.timeSetting.getSelectedItem() );
  }

  public ArrayList<Channels> createChanelsList()
  {
    for(int i = 0; i < channelList.size(); i++)
      channelList.get(i).destroy();

    channelList.clear();

    for(int i = 0; i < nameOfStream.length; i++)
    {
      String channelName = nameOfChannel[i].getText().trim();
      String stream = nameOfStream[i].getText().trim();
      int time = timeSetting.getTime();

      if(stream.compareTo("") == 0)
	stream = channelName;

      if(channelName.compareTo("") != 0)
	channelList.add(new Channels(stream, serverType[i].getSelectedItem(channelName), time));

    }

    return channelList;
  }

  public void saveSettings()
  {
    try
    {
      FileOutputStream fos = new FileOutputStream(fileSettingsName);	
      ObjectOutputStream os = new ObjectOutputStream(fos);
      os.writeObject(this);
      os.close();
      fos.close();
    }catch( IOException e ) 
    {
      e.printStackTrace();
    }
  }
}
